package cn.hs.ph.serviet;

import cn.hs.ph.dao.UserDao;
import cn.hs.ph.dao.impl.UserDaoimpl;
import cn.hs.ph.entity.User;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/login")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("pwd");
//简介数据库
        try {
            User user = new UserDaoimpl().selectByUsernameAndPwd(username, password);
       if (user == null) {
           Result success=Result.error();
           resp.getWriter().write(JSON.toJSONString(success));

       }else {
           Result result = Result.success("成功");
           resp.getWriter().write(JSON.toJSONString(result));

       }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp );
    }
}
